import base64
import hashlib
import urllib.parse

from Crypto import Random
from Crypto.Hash import SHA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
from Crypto.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5
from Crypto.PublicKey import RSA

# # 伪随机数生成器
# random_generator = Random.new().read
# # rsa算法生成实例
# rsa = RSA.generate(2048, random_generator)

# # master的秘钥对的生成
# private_pem = rsa.exportKey()

# with open('master-private.pem', 'wb') as f:
#     f.write(private_pem)

# public_pem = rsa.publickey().exportKey()
# with open('master-public.pem', 'wb') as f:
#     f.write(public_pem)

# # ghost的秘钥对的生成
# private_pem = rsa.exportKey()
# with open('master-private.pem', 'wb') as f:
#     f.write(private_pem)

# public_pem = rsa.publickey().exportKey()
# with open('master-public.pem', 'wb') as f:
#     f.write(public_pem)


#s = '0XGp1K4fORp8ezteL/eSqKmNDa1kXyAKRcaIEKQlLZaAxIfmGoMs1K26odIUmJVHC4MJrgj0zdp0\ntAkIL2W5aDFx5+8iM5sh332AIr6/pUjRC5QVRVLYQ27BLU/rVUeeEYU5PNkwc1064bOLZGkX0onH\nG9pV2bi+tCfdh5PipNZcKBxrYtJaioca4PE/NW0mAarusaSe0meUl/VnCZbuDy59sdZ0Lc3YKmH0\nNG4rNqz1M5+m97Uf7wzhyaEs/SfmOXDUNil0zeQ+mSxOkBaLM/eJBM3VdwUM6KxUUUGcuUAxkL8G\nALggHhWN3O8HUM+xzq/2pg4uXIns6cgvK/ipRA\u003d\u003d\n'
#pk = 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDtpJHqfU/s90SrEhZ3hMeKo6SgsQzrwoSfUEl7Nckjq6JjJy+hNpOQ6Bio3fbAN2XYQoJy2IKLTFBeawdpFWahQPFtVRleZ+FqPvslmyPMONYJCA0viCEN5yQwmaWZ2PbKNesMEtYJJz6rGrU7qVADfrB6/77HZgNCkX3XW3ogexB35iatV6xQ1dGCO7GY0zW6arnAcBuNYBfGsQ/TOF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xJ2i/WrGl3v8viFFYe2gU47OlSeS6yf0rzn256nxQuTFyk1Secv9NNA26YpIGs2SBAk+/WLX+28pxieYxdlgdJGnQ5EDM/kkrM2iBek0weHGmgBj3BoEDrdAoGAFAFS8LPdBnGdo09uBX07W02h+RuwoOtdanp1LMQmUub4iVWra1Xw7d1qRxbPIJyXmPS+Bky/DSmHbN3KX6S5+0BhkPvsm0Gy5brzC5WCLDo8I3Sb4Zi7ES5mMqhKHAiX9dN5ALdRBf9548yNod/BvCCEphnKUddHUdfyKQMWP/Y='

if __name__ == '__main__':
    request = {
        "android_id": "04d4c4248cc29967",
        "imei": "863254010244902",
        "introduction": "0",
        "isVM": "1",
        "mac": "08:00:27:D0:5A:66",
        "pfmId": "11",
        "pid": "103866",
        "platform": "Android",
        "signType": "RSA",
        "tel": "13205776136",
        "timestamp": "2020-12-08 17:51:54",
        "unit": "VOG-AL10",
        "version": "1.0",
        "sign": "0XGp1K4fORp8ezteL/eSqKmNDa1kXyAKRcaIEKQlLZaAxIfmGoMs1K26odIUmJVHC4MJrgj0zdp0\ntAkIL2W5aDFx5+8iM5sh332AIr6/pUjRC5QVRVLYQ27BLU/rVUeeEYU5PNkwc1064bOLZGkX0onH\nG9pV2bi+tCfdh5PipNZcKBxrYtJaioca4PE/NW0mAarusaSe0meUl/VnCZbuDy59sdZ0Lc3YKmH0\nNG4rNqz1M5+m97Uf7wzhyaEs/SfmOXDUNil0zeQ+mSxOkBaLM/eJBM3VdwUM6KxUUUGcuUAxkL8G\nALggHhWN3O8HUM+xzq/2pg4uXIns6cgvK/ipRA\u003d\u003d\n"
    }

    gameKey = '1ebab6f0ee90b9f358e1f3362019a5a9'  # 103866
    keys = sorted(request.keys())
    print('keys:' + str(keys))

    signParams = ''
    for k in keys:
        if request[k] != '' and request[k] != None and k != 'sign' and '@' != str(request[k])[0]:
            # signParams += k + '=' + urllib.parse.quote(request[k]) + '&'
            signParams += k + '=' + request[k] + '&'
    signParams += 'key=' + gameKey

    print('signParams:' + signParams)
    
    m = hashlib.md5(signParams.encode())

    sign_md5 = m.hexdigest()

    print('sign md5:' + sign_md5)
    print('----')

    ## "sign": "0XGp1K4fORp8ezteL/eSqKmNDa1kXyAKRcaIEKQlLZaAxIfmGoMs1K26odIUmJVHC4MJrgj0zdp0\ntAkIL2W5aDFx5+8iM5sh332AIr6/pUjRC5QVRVLYQ27BLU/rVUeeEYU5PNkwc1064bOLZGkX0onH\nG9pV2bi+tCfdh5PipNZcKBxrYtJaioca4PE/NW0mAarusaSe0meUl/VnCZbuDy59sdZ0Lc3YKmH0\nNG4rNqz1M5+m97Uf7wzhyaEs/SfmOXDUNil0zeQ+mSxOkBaLM/eJBM3VdwUM6KxUUUGcuUAxkL8G\nALggHhWN3O8HUM+xzq/2pg4uXIns6cgvK/ipRA\u003d\u003d\n"
    Private = 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCyOoR49HbG8IXNWCE+W7D/SPpTKeedaZlB5401rNdpSeMANXEkXBfEwf1A7UyIoUrWX5JJtOuxQD0Wu3fcAiRMaxLiLiOUnwV15olAqFVbI/XFzmLG2lpEWgybEaq/FGULndzGdBFtTixT39va3tMZ3ncwtY3eZbQ7EVrulzvhJlUZ79yZlP9eOMlqX++7N/MTvmotXzR46G2NoI1kPOy6wXkJ/HI9plPHuPRIw/DN8zRHfCxo+yYQ07n6+Sfjud3KHqKvy3jkFfl5wOf5qHMXYiemvVYCLC9MIZ1WlXVS6BP1fXI74MOTIJgw0R6tVi7VqnxXBchjZWt0uu2kZw8bAgMBAAECggEAUSL4qcPIyto2X3wObj/5vM3xP6jlCzFGls0d6OCNlB4R3Ed4AXB3VZhmfYdJ/y1GT39krm9RmPjAlrezYhILikp4Z0VP51nZd4oC08+ORBaDzZAeQZ6n11QTRwjs7V4QfhBYsHOod1VOMsTHZsVn3u/63M7aDfPI79b/yXxJwz4YBmiQQQczv2w3G035Q0J4Huqj0jqZ8ioew418U6svFI8zGkHg0qJ2ysZxXqqNXzJtt2WSIKIw9Kc2dOoMBr5I97kbkY8bqAkxVRedDGANOllA8q6PtWZwlTnrVpauDzO4Le7pze1u76rfnPTIvo9EMm0GVNyRWTsVwquJMIjwCQKBgQDnZY0kax45fnYiX1MfJb7eOGKts/79NznXfoDEPD+xzK1rXsPLVdpO+LdraO0J43v4aslgqPT1hTh5/My4wrbj6cgOr5KO9vucWkdX9f+x1dcmCtDlJPx2Q5+J60ogQgrHtTFIY/evu5UoagzcNIVZfgempcTgJj3HRJjurOzeVQKBgQDFLcTvX/yhjlB3h0Ci4/yx5PQTv26llBJV3MWApA8oNaECM4pP6WP1hk9MeaD/stcE3MghlE/vJ4wn5/9Geelqy8Ugi06NULlkXv0GCh6WCoMO5Gy3ZXGjdvPMHE90NtXNxMNMN+YVhdwVJEyvG2ZD7BX6mSSkIN6lmxy8wAvHrwKBgB8iaNVHnG979oU1cke0mnPn/k8E8wA7GAtIuCavPJq9wW+69nk7ef46NOO84Hf+JcYNCD97PEp0vF6dhyYs/NK5t/NR6FRYIw6sjwDk1pyk+yfhOK48RcPb4XF7OSQR/sVUOedVnBQHF1CyxDw0zOR3sqvXjh/ixVk0f6dNmSlpAoGBAJtUzwt0JmRu5F0FI5ou2Gab4tubtuKhn0Acx0nr9DTM6qX9TDxfs+s7f0TTnE1S6ahe9Rne6ImSMgtN6//tEkCbfhOBKsSQjzvOyKz1U9sHNO+Ka0qek8MedjiZJM58VDnhg6iLcKoL5bCSBAb+IrXnSfk8o2zYqAz2ANGAyMbpAoGAKRmqpUjyucNpKSosZ1rPh91AvXH+l6pFY9ifx/+KSt4geWeAS21ciKFIVi38S8deoGLX1/ydHkDhiHD+pjIVXNs73JGwm3qZ7bJmn6w4J8dnyYvnTOyxbj9Gs7MvqyXemtT44Q684ctmraizVlGwkvEW+LS1Rr/BlIH4GjrYiuc'
    with open("master-public.pem") as f:
        key = f.read()
    rsakey = RSA.importKey(key)
    signer = Signature_pkcs1_v1_5.new(rsakey)
    digest = SHA.new()
    digest.update(sign_md5.encode())
    sign = signer.sign(digest)
    signature = base64.b64encode(sign)
    print(signature.decode())

    

